package com.sunwayworld.basemodule.coredata.productreport.service.impl;

import com.sunwayworld.basemodule.coredata.productreport.bean.LimsProductReportTemplateBean;
import com.sunwayworld.basemodule.coredata.productreport.bean.LimsReportClientProductBean;
import com.sunwayworld.basemodule.coredata.productreport.dao.LimsProductReportTemplateDao;
import com.sunwayworld.basemodule.coredata.productreport.service.LimsProductReportTemplateService;
import com.sunwayworld.basemodule.coredata.productreport.service.LimsReportClientProductService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsProductReportTemplateServiceImpl implements LimsProductReportTemplateService {

    @Autowired
    private LimsProductReportTemplateDao limsProductReportTemplateDao;

    @Lazy
    @Autowired
    private LimsReportClientProductService clientProductService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsProductReportTemplateDao getDao() {
        return limsProductReportTemplateDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsProductReportTemplateBean limsProductReportTemplate = jsonWrapper.parseUnique(LimsProductReportTemplateBean.class);
        limsProductReportTemplate.setId(ApplicationContextHelper.getNextIdentity());
        limsProductReportTemplate.setTemplateCode(ApplicationContextHelper.getNextSequence(getDao().getTable()));
        getDao().insert(limsProductReportTemplate);
        return limsProductReportTemplate.getId();
    }

    @Override
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsProductReportTemplateBean> parse = wrapper.parse(LimsProductReportTemplateBean.class);
        if(CollectionUtils.isEmpty(parse)) return;
        List<Long> idList = parse.stream().map(LimsProductReportTemplateBean::getId).collect(Collectors.toList());
        List<LimsReportClientProductBean> reportClientProductBeans = idList.stream().map(id -> {
            LimsReportClientProductBean productBean = new LimsReportClientProductBean();
            productBean.setReportTemplateId(id);
            return productBean;
        }).collect(Collectors.toList());

        clientProductService.getDao().deleteBy(reportClientProductBeans, "reportTemplateId");
        getDao().deleteByIdList(idList);

    }
}
